package com.jinlejia.erp.ui.home;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.jinlejia.erp.R;
import com.jinlejia.erp.databinding.ActivityPuzzleSaveBinding;
import com.jinlejia.erp.ui.common.MainActivity;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;


/**
 * 保存成功（Activity）
 */

public class PuzzleSaveActivity extends AppCompatActivity {
    private ActivityPuzzleSaveBinding binding;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_puzzle_save);
        binding.backBtn.setOnClickListener(this::onClick);
        binding.home.setOnClickListener(this::onClick);
        binding.shareWechat.setOnClickListener(this::onClick);
        binding.shareFriends.setOnClickListener(this::onClick);
        binding.shareQq.setOnClickListener(this::onClick);
        initView();
    }

    /**
     * 初始化视图
     */
    private void initView() {
        if (!TextUtils.isEmpty(getIntent().getStringExtra("image"))){
            Bitmap bitmap = BitmapFactory.decodeFile(getIntent().getStringExtra("image"));
            binding.saveImage.setImageBitmap(bitmap);
        }
    }


    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back_btn:         //返回
                finish();
                break;
            case R.id.home:             //首页
                startActivity(new Intent(PuzzleSaveActivity.this, MainActivity.class));
                break;
            case R.id.share_wechat:     //微信
                Wechat.ShareParams wechatSp = new Wechat.ShareParams();
                wechatSp.setShareType(Platform.SHARE_IMAGE);
                wechatSp.setImagePath(getIntent().getStringExtra("image"));
                Platform wechat = ShareSDK.getPlatform( Wechat.NAME);
                wechat.setPlatformActionListener(new OneKeyShareCallback());
                wechat.share(wechatSp);
                break;
            case R.id.share_friends:    //朋友圈
                WechatMoments.ShareParams momentsSp = new WechatMoments.ShareParams();
                momentsSp.setShareType(Platform.SHARE_IMAGE);
                momentsSp.setImagePath(getIntent().getStringExtra("image"));
                Platform wechatMoments = ShareSDK.getPlatform( WechatMoments.NAME);
                wechatMoments.setPlatformActionListener(new OneKeyShareCallback()); // 设置分享事件回调
                // 执行分享
                wechatMoments.share(momentsSp);
                break;
            case R.id.share_qq:         //QQ分享
                QQ.ShareParams qqSp = new QQ.ShareParams();
                qqSp.setImagePath(getIntent().getStringExtra("image"));
                Platform qq = ShareSDK.getPlatform(QQ.NAME);
                qq.setPlatformActionListener(new OneKeyShareCallback());
                qq.share(qqSp);
                break;
        }
    }
    static class OneKeyShareCallback implements PlatformActionListener {

        @Override
        public void onError(Platform platform, int action, Throwable t) {
            t.printStackTrace();
        }

        @Override
        public void onComplete(Platform platform, int action,
                               HashMap<String, Object> arg2) {
        }

        @Override
        public void onCancel(Platform platform, int action) {
        }

    }
}
